//
//  KABCosDetComTCell.m
//  kabu
//
//  Created by XiangDongMBP on 2017/2/9.
//  Copyright © 2017年 East. All rights reserved.
//

#import "KABCosDetComTCell.h"
#import "KABModel.h"
#import <YYWebImage.h>
#import "NSString+XDNSDate.h"
#import "UIColor+XDHex.h"
#import "GlobalHeader.h"
#import "TTTAttributedLabel.h"

@interface KABCosDetComTCell ()<TTTAttributedLabelDelegate>

@end

@implementation KABCosDetComTCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    
    self.portraitImgView.layer.masksToBounds = YES;
    self.portraitImgView.layer.cornerRadius = CGRectGetWidth(self.portraitImgView.bounds) / 2.0;
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(portraitImgViewTapped:)];
    [self.portraitImgView addGestureRecognizer:tapGesture];
    
    NSDictionary *atts = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:KABGlobalColorTabBarItemColor]};
    self.nameLabel.linkAttributes = @{NSForegroundColorAttributeName : [UIColor xd_colorWithHex:KABGlobalColorTabBarItemColor],
                                      NSFontAttributeName : [UIFont systemFontOfSize:16.0]};
    self.nameLabel.delegate = self;
    self.nameLabel.activeLinkAttributes = nil;
    self.nameLabel.inactiveLinkAttributes = nil;
    self.replyLabel.linkAttributes = atts;
    self.replyLabel.activeLinkAttributes = nil;
    self.replyLabel.inactiveLinkAttributes = nil;
    self.replyLabel.font = [UIFont systemFontOfSize:14.0];
    self.replyLabel.delegate = self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)portraitImgViewTapped:(UIGestureRecognizer *)gesture {
    if ([self.delegate respondsToSelector:@selector(kABCosDetComTCell:portraitImgViewTapped:)]) {
        [self.delegate kABCosDetComTCell:self portraitImgViewTapped:(UIImageView *)gesture.view];
    }
}

- (void)configViewWithCosDetComItem:(KABCosDetComItem *)item {
    [self.portraitImgView yy_setImageWithURL:[NSURL URLWithString:item.commentator_imageurl] placeholder:[KABDefaultVarModel sharedInstance].smallDefaultImg options:YYWebImageOptionSetImageWithFadeAnimation completion:nil];
    
    NSString *name = item.commentator.length ? item.commentator : @"";
    NSString *commentatorIdStr = [NSString stringWithFormat:@"%ld", item.commentator_id];
    NSAttributedString *nameAStr = [[NSAttributedString alloc] initWithString:name
                                                                   attributes:@{NSLinkAttributeName : commentatorIdStr}];
    self.nameLabel.text = nameAStr;
    
    self.timeLabel.text = [item.time xd_yyyyMMddHHmmssStrToFriendDateStr];
    
    NSString *str = item.content.length ? item.content : @"";
    NSMutableAttributedString *mAStr = [[NSMutableAttributedString alloc] initWithString:str];
    if (item.reply_which_nick_name.length) {
        str = [NSString stringWithFormat:@"回复%@：%@", item.reply_which_nick_name, item.content];
        mAStr = [[NSMutableAttributedString alloc] initWithString:str];
        NSString *replyWhichUserIdStr = [NSString stringWithFormat:@"%ld", item.reply_which_user_id];
        [mAStr addAttributes:@{NSLinkAttributeName : replyWhichUserIdStr} range:[str rangeOfString:item.reply_which_nick_name]];
    }
    [mAStr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14.0]} range:[str rangeOfString:str]];
    self.replyLabel.text = mAStr;
}

#pragma mark - TTTAttributedLabelDelegate
- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if ([self.delegate respondsToSelector:@selector(kABCosDetComTCell:nickNameTapped:)]) {
        [self.delegate kABCosDetComTCell:self nickNameTapped:url];
    }
}

@end
